package proj2501.simple_module;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;

import proj2501.util.DocumentAdapter;
import proj2501.util.SelectableLabel;
import proj2501.util.UU;

class P005MD5 extends JFrame {
	private BrowseButton browseButton;
	private File file;
	private JLabel fileLabel;
	private String md5String;
	private SelectableLabel md5Label;
	private MD5Field md5Field;
	private String sha1String;
	private SelectableLabel sha1Label;
	private SHA1Field sha1Field;
	
	public P005MD5() {
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setTitle("MD5 Test");
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		browseButton = new BrowseButton();
		add(browseButton);
		fileLabel = new JLabel("Selected File: None");
		add(fileLabel);
		md5Label = new SelectableLabel("MD5: None");
		add(md5Label);
		md5Field = new MD5Field();
		add(md5Field);
		sha1Label = new SelectableLabel("SHA-1: None");
		add(sha1Label);
		sha1Field = new SHA1Field();
		add(sha1Field);
		setVisible(true);
		pack();
	}
	
	private class BrowseButton extends JButton {
		public BrowseButton() {
			super("Browse");
			addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					System.out.println(999);
					JFileChooser chooser = new JFileChooser();
					int result = chooser.showOpenDialog(P005MD5.this);
					if (result == JFileChooser.APPROVE_OPTION) {
						file = chooser.getSelectedFile();
						fileLabel.setText("Selected File: " + file.getAbsolutePath());
						md5String = UU.getMD5String(file);
						md5Label.setText("MD5: " + md5String);
						sha1String = UU.getSHA1String(file);
						sha1Label.setText("SHA-1: " + sha1String);
					}
				}
			});
		}
	}
	
	private abstract class MessageDigestField extends JTextField {
		private boolean isFirstFocus = true;
		private boolean ignoreEvent;

		public MessageDigestField(String text) {
			super(text);
			setMaximumSize(new Dimension(Integer.MAX_VALUE, getPreferredSize().height));
			getDocument().addDocumentListener(new DocumentAdapter() {
				public void insertUpdate(DocumentEvent e) {
					super.insertUpdate(e);
					if (! ignoreEvent) {
						update();
					}
				}
				@Override
				public void removeUpdate(DocumentEvent e) {
					super.removeUpdate(e);
					if (! ignoreEvent) {
						update();
					}
				}
			});
			addFocusListener(new FocusAdapter() {
				@Override
				public void focusGained(FocusEvent e) {
					super.focusGained(e);
					if (isFirstFocus) {
						ignoreEvent = true;
						setText("");
						ignoreEvent = false;
						isFirstFocus = false;
					}
				}
			});
		}
		
		abstract protected void update();
	}
	
	private class MD5Field extends MessageDigestField {
		public MD5Field() {
			super("(Enter MD5 code here.)");
		}
		
		@Override
		protected void update() {
			if (getText().toUpperCase().equals(md5String)) {
				setBackground(Color.GREEN);
			} else {
				setBackground(Color.RED);
			}
		}
	}
	
	private class SHA1Field extends MessageDigestField {
		public SHA1Field() {
			super("(Enter SHA-1 code here.)");
		}
		
		@Override
		protected void update() {
			if (getText().toUpperCase().equals(sha1String)) {
				setBackground(Color.GREEN);
			} else {
				setBackground(Color.RED);
			}
		}
	}
}
